package org.tarry.springbootwebvalidation.validation.validator;

import lombok.extern.slf4j.Slf4j;
import org.tarry.springbootwebvalidation.validation.annotation.CustomerCode;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
public class CustomerCodeValidator implements ConstraintValidator<CustomerCode,String> {

    boolean canNull;

    @Override
    public void initialize(CustomerCode constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        log.debug("CustomerCodeValidator ~~~");
        canNull = constraintAnnotation.canNull();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (canNull){
            return true;
        }
        log.debug("validator customer code");
        String reg = "\\d{17}";
        return value.matches(reg);
    }
}
